<?php
namespace Common\Model;

/**
 * 客栈表
 * @modify trainyao 2016-04-29 创建
 * @index  isdelete_hotelentity_id(`isdelete`, `hotelentity_id`);
 */

Class HotelPartnerPayModel extends BaseModel
{
    protected $trueTableName = 'bk_hotel_partner_pay';
    protected $field = 'id,hotelentity_id,wechatappid,wechatappsecret,wechatmchid,wechatmchkey,wechatmpverify,aliaccount,alipid,alikey';

    protected function _before_insert(&$data, $options) {
        $data['operator'] = getUserId();
    }

    /**
     * 新建客栈自收支付信息条目
     * @modify trainyao 2016-07-11       创建
     * @param  integer   $hotelId   客栈ID
     * @param  string   $wechatappid     微信支付appid
     * @param  string   $wechatappsecret 微信支付appsecret
     * @param  string   $wechatmchid     微信支付mchid
     * @param  string   $wechatmchkey    微信支付mchkey
     * @param  string   $aliaccount      支付宝支付account
     * @param  string   $alipid          支付宝支付pid
     * @param  string   $alikey          支付宝支付key
     */
    public function addInfo($hotelId, $wechatappid = null, $wechatappsecret = null, $wechatmchid = null, $wechatmchkey = null, $aliaccount = null, $alipid = null, $alikey = null, $wechatmpverify = null) {
        $data = array(
            'hotelentity_id' => $hotelId,
        );
        if (!is_null($wechatappid)) {
            $data['wechatappid'] = $wechatappid;
        }
        if (!is_null($wechatappsecret)) {
            $data['wechatappsecret'] = $wechatappsecret;
        }
        if (!is_null($wechatmchid)) {
            $data['wechatmchid'] = $wechatmchid;
        }
        if (!is_null($wechatmchkey)) {
            $data['wechatmchkey'] = $wechatmchkey;
        }
        if (!is_null($aliaccount)) {
            $data['aliaccount'] = $aliaccount;
        }
        if (!is_null($alipid)) {
            $data['alipid'] = $alipid;
        }
        if (!is_null($alikey)) {
            $data['alikey'] = $alikey;
        }
        if (!is_null($wechatmpverify)) {
            $data['wechatmpverify'] = $wechatmpverify;
        }
        $id = $this->add($data);
        return empty($id) ? false : $id;
    }

    /**
     * 根据客栈获取payDetail信息
     * @modify trainyao 2016-04-29 创建
     * @param  integer   $hotelId   客栈ID
     * @return array              信息数组
     */
    public function getInfoByHotel($hotelId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );
        $data = $this->where($option)->field($this->field)->find();

        return empty($data) ? array() : $data;
    }

    /**
     * 根据客栈ID保存信息
     * @modify trainyao 2016-07-11       创建
     * @param  integer   $hotelId         客栈ID
     * @param  string   $wechatappid     微信支付appid
     * @param  string   $wechatappsecret 微信支付appsecret
     * @param  string   $wechatmchid     微信支付mchid
     * @param  string   $wechatmchkey    微信支付mchkey
     * @param  string   $aliaccount      支付宝支付account
     * @param  string   $alipid          支付宝支付pid
     * @param  string   $alikey          支付宝支付key
     * @return boolean                    保存结果
     */
    public function saveInfoByHotel($hotelId, $wechatappid = null, $wechatappsecret = null, $wechatmchid = null, $wechatmchkey = null, $aliaccount = null, $alipid = null, $alikey = null, $wechatmpverify = null) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );
        $data = array();
        if (!is_null($wechatappid)) {
            $data['wechatappid'] = $wechatappid;
        }
        if (!is_null($wechatappsecret)) {
            $data['wechatappsecret'] = $wechatappsecret;
        }
        if (!is_null($wechatmchid)) {
            $data['wechatmchid'] = $wechatmchid;
        }
        if (!is_null($wechatmchkey)) {
            $data['wechatmchkey'] = $wechatmchkey;
        }
        if (!is_null($aliaccount)) {
            $data['aliaccount'] = $aliaccount;
        }
        if (!is_null($alipid)) {
            $data['alipid'] = $alipid;
        }
        if (!is_null($alikey)) {
            $data['alikey'] = $alikey;
        }
        if (!is_null($wechatmpverify)) {
            $data['wechatmpverify'] = $wechatmpverify;
        }

        return $this->where($option)->save($data) === false ? false : true;
    }
}
